<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Threed;
use DB;



class ThreedController extends Controller
{
    public function index(Request $request)
    {
    	$where = [];
    	$data = $request->except(['token','_method','page']);
        $where = array_where($data, function ($value, $key) {
            return $where[$key] = $value;
        });       
       
    	$list = Threed::where($where)->paginate(10);    	
    	return view('admin.threed.index',compact('list','where'));
    }


    public function create(Request $request)
    {      
        $list = Threed::get();
        return view('admin.threed.add',compact('list'));
    }

    public function store(Request $request)
    {
        $rs['status'] = 'danger';
        $rs['msg']    = '操作失败';
        $data = $request->except(['_token']);
        
        $file_arr = [
                '东'=>'pic1',
                '南'=>'pic2',
                '西'=>'pic3',
                '北'=>'pic4',
                '上'=>'pic5',
                '下'=>'pic6'
        ];
      
        $savepath = '/public/pic/';

        foreach ($file_arr as $k=>$val) {
            // 文件是否上传成功
            if (array_get($data,$val)) { 
                $file = $request->file($val);         
                $filename = uploadFile($file,$savepath);
                $data[$val] = '/storage/pic/'.$filename;      
            } else {
                $rs['msg'] = '没有上传图片：'.$k;
                return back()->with('rs',$rs);
            }
        }
        

        $flag = Threed::create($data);
        if($flag) {
            $rs['status'] = 'success';
            $rs['msg'] = '操作成功';
            return redirect('zadmin/threeds')->with('rs',$rs);
        }
        return back()->with('rs',$rs);
    }

    public function edit($id)
    {
        $list = Threed::where('id','!=',$id)->get();
        $data = Threed::find($id);
        return view('admin.threed.edit',compact('data','list'));
    }

    public function update(Request $request, $id)
    {
        $rs['status'] = 'success';
        
        $data = $request->except(['_token','_method']);
        $file_arr = [
                '东'=>'pic1',
                '南'=>'pic2',
                '西'=>'pic3',
                '北'=>'pic4',
                '上'=>'pic5',
                '下'=>'pic6'
        ];
      
        $savepath = '/public/pic/';

        foreach ($file_arr as $k=>$val) {
            // 文件是否上传成功
            if (array_get($data,$val)) { 
                $file = $request->file($val);         
                $filename = uploadFile($file,$savepath);
                $data[$val] = '/storage/pic/'.$filename;      
            }
        }
        $flag = Threed::where('id',$id)->update($data);
        return redirect('zadmin/threeds')->with('rs',$rs);
    }

    public function destroy($id)
    {
        $rs['status'] = 'success';
        Threed::destroy($id);
        return redirect('zadmin/threeds')->with('rs',$rs);
    }

   

   
}
